import drawQrcode from '/weapp.qrcode.esm.js';
import barcode from '/barcode.js';
const { HttpRequest, globalData, UserAccess, addUserStat } = getApp();
const app = getApp();
Page({
  data: {
    activeWrap: false,
    loadingActive: false,
  },
  async onLoad() {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    let Screensize = globalData.rate > globalData.limit ? '1624' : '1334';
    this.setData({
      Screensize
    })
    this.setData({
      loadingActive: true
    })
    await this.userInfo()
    await this.qrCode()
    this.setData({
      loadingActive: false
    })


    // 用户行为记录埋点

    let routerArr = getCurrentPages();
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
    }
    let track = UserAccess(UserAccessoptions)


    // 自定义埋点
    let customAccessoptions = {
      customType: '会员码'
    }
    console.log('会员码');

    let tracks = addUserStat(customAccessoptions)

  },
  async userInfo() {
    //用户信息查询
    let bodyObj1 = {
      // "param": "13800138000",
      // "type": 0,
      "param": globalData.ouid,
      "type": 3
    }
    let path1 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/member/api/tmall/query`;
    let res1 = await HttpRequest(path1, 'POST', '', bodyObj1);
    if (res1.data.resultCode == 0) {
      // res1.data.data.fullName = res1.data.data.fullName!=''? res1.data.data.fullName: '会员';
      globalData.userInfo = res1.data.data;   //存储用户信息
      this.setData({
        menuuserInfo: res1.data.data
      })
    }
    //积分查询 
    let bodyObj2 = {
      // "param": "13800138000",
      // "type": 0,
      "param": globalData.ouid,
      "type": 3
    }
    let path2 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/points/api/tmall/account`;
    let res2 = await HttpRequest(path2, 'POST', '', bodyObj2);
    if (res2.data.resultCode == 0) {
      globalData.userInfo.availablePoint = res2.data.data.availablePoint;
      globalData.userInfo.willExpirePoint = res2.data.data.willExpirePoint;
      this.setData({
        menuavailablePoint: res2.data.data.availablePoint,
        menuwillExpirePoint: res2.data.data.willExpirePoint
      })
    }
  },
  navChange() {
    this.setData({
      activeWrap: true
    })
  },
  //二维码条形码构建
  qrCode() {
    let text = `${globalData.userInfo.memberId}|${globalData.userInfo.code}`;
    drawQrcode({
      width: this.convert_length(280),
      height: this.convert_length(280),
      canvasId: 'myQrcode',
      text,
    })
    console.log(text)
    barcode.code128(my.createCanvasContext('barcode'), text, this.convert_length(750), this.convert_length(118));
  },
  convert_length(length) {
    return Math.round(my.getSystemInfoSync().windowWidth * length / 750);
  },
  navExit() {
    this.setData({
      activeWrap: false
    })
  },
});
